package nl.vu.few.ibivu.biomodels.client.ui;

import nl.vu.few.ibivu.biomodels.client.ui.view.ModelDetailsView;
import nl.vu.few.ibivu.biomodels.shared.UserProfile;

import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.CheckboxItem;
import com.smartgwt.client.widgets.form.fields.TextAreaItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.BlurEvent;
import com.smartgwt.client.widgets.form.fields.events.BlurHandler;
import com.smartgwt.client.widgets.form.fields.events.ChangedEvent;
import com.smartgwt.client.widgets.form.fields.events.ChangedHandler;

public class ModelDetailsForm extends DynamicForm implements ModelDetailsView {

	private Presenter presenter;
	
	private TextItem nameText;
	private boolean nameChanged;
	private TextItem ownerText;
	private boolean descriptionChanged;
	private TextAreaItem descriptionText;
	private CheckboxItem isPublicBox;
	
	public ModelDetailsForm() {
		presenter = null;
		
		nameChanged = false;
		nameText = new TextItem();
		nameText.setTitle("Name");
		nameText.setRequired(true);
		nameText.addChangedHandler(new ChangedHandler() {
			@Override
			public void onChanged(ChangedEvent event) {
				nameChanged = true;
			}});
		
		nameText.addBlurHandler(new BlurHandler(){

			@Override
			public void onBlur(BlurEvent event) {
				if (nameChanged && nameText.validate()) {
					presenter.setName(nameText.getValueAsString());
					nameChanged = false;
				}
				
			}});
		
		ownerText = new TextItem();
		ownerText.setTitle("Owner");
		ownerText.setDisabled(true);
		
		descriptionChanged = false;
		descriptionText = new TextAreaItem();
		descriptionText.setTitle("Description");
		descriptionText.addChangedHandler(new ChangedHandler() {
			@Override
			public void onChanged(ChangedEvent event) {
				descriptionChanged = true;
			}});
		
		descriptionText.addBlurHandler(new BlurHandler(){

			@Override
			public void onBlur(BlurEvent event) {
				if (descriptionChanged && descriptionText.validate()) {
					presenter.setDescription(descriptionText.getValueAsString());
					descriptionChanged = false;
				}
				
			}});
		
		isPublicBox = new CheckboxItem();
		isPublicBox.setTitle("Make it public");
		isPublicBox.addChangedHandler(new ChangedHandler() {

			@Override
			public void onChanged(ChangedEvent event) {
				presenter.setPublic((Boolean)event.getValue());
				
			}});
		
		this.setFields(nameText, descriptionText, isPublicBox, ownerText);
	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
		descriptionChanged = false;
		nameChanged = false;
		String name = presenter.getName();
		if (name == null || name.isEmpty()) {
			name = "My Model";
			presenter.setName(name);
		}
		nameText.setValue(name);
		descriptionText.setValue(presenter.getDescription());
		isPublicBox.setValue(presenter.isPublic());
		UserProfile owner = presenter.getOwner();
		if (owner == null) {
			ownerText.setValue("Model not saved yet!");
		}
		else {
			ownerText.setValue(presenter.getOwner().toString());
		}
	}

}
